import cookies from '../libs/util.cookies'

export default {
  data: () => ({
    userMenu: false,
    user: {}
  }),
  created () {
    const user = JSON.parse(localStorage.getItem('milk-user'))

    // 不太可能出现有token 无 user 情况
    if (!user) {
      this.signOut()
    } else {
      this.$store.commit('milk/user/setUser', { user })
      this.user = user
    }
  },
  methods: {
    signOut () {
      localStorage.removeItem('milk-token')
      cookies.remove('token')
      this.userMenu = false
      this.$router.push({ name: 'Login' })
    },
  }
}
